<?
if(REWRITE_ON){
	ob_start();//start buffering//@ob_start('ob_gzhandler');
}

require_once ROOT_PATH.'core/AutoLoader.php';

CGlobal::$my_server				=	$server_list;
CGlobal::$img_server			=	$img_server;
CGlobal::$img_ftp_server		=	$img_ftp_server;

define('CPC_URL',WEB_DIR.AZRewrite::formatUrl("?page=cpc_ad"));

if(MEMCACHE_ON){
	CGlobal::$memcache_server = $memcache_server;
}
unset($memcache_server,$server_list,$img_server,$img_ftp_server);

if(isset($_REQUEST['trigger']) && (int)$_REQUEST['trigger']==1)
{
	//Xoá cache tự động
	AZArrCache::del_cache();
	AZCache::auto_delete();
	AZNet::del_page_cache();
	StaticCache::delCache();
	exit;
}

$is_search_engine_array = array("Google", "Fast", "Slurp", "Ink", "Atomz", "Scooter", "Crawler", "MSNbot", "Poodle", "Genius"); 
$is_search_engine = 0; 
foreach($is_search_engine_array as $key => $val){
	if(strstr($_SERVER['HTTP_USER_AGENT'], $val)) 
		$is_search_engine++; 
}

if(isset($_GET['page']) && $_GET['page']=='error'){
	define('ERROR_PAGE',1);
}
else{
	define('ERROR_PAGE',0);
}

if($is_search_engine == 0 && !defined('NO_SESSION') && !ERROR_PAGE){
	if(SESSION_TYPE == 'db'){
		require_once ROOT_PATH.'includes/session.class.php';//Session db store
	}
	elseif(SESSION_TYPE == 'memcache'){
		require_once(ROOT_PATH."includes/memcache.session.php");//Session memcache store
	}
	else{		
		session_start(); 
	}
}

if(isset($_GET['type'])){	
	CGlobal::$curItemType=(int)$_GET['type'];
}

// Disable ALL magic_quote
set_magic_quotes_runtime(0);

if (get_magic_quotes_gpc()){
	function stripslashes_deep($value){
		$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
		return $value;
	}
	$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
	$_COOKIE  = array_map('stripslashes_deep', $_COOKIE);
}

if(!ERROR_PAGE){
	//get all config 
	AZLib::get_config();
	
	CGlobal::$exchange = (int)@CGlobal::$configs['exchange']['conf_val'];
	
	//Get all Categories
	AZLib::getCats();
	
	//if((isset($_GET['category_id']) && $_GET['category_id']) || (isset($_GET['zone_id']) && $_GET['zone_id']) || (Url::get("page","home") == 'home')){
	if(isset($_GET['category_id']) && $_GET['category_id']){
		//AZLib::getCats();
	
		if(isset($_GET['category_id']) && isset(CGlobal::$allCategories[(int)$_GET['category_id']]) && CGlobal::$allCategories[(int)$_GET['category_id']]){
			CGlobal::$curCategory = (int)$_GET['category_id'];
		}
	}
	
	if(isset($_GET['province_id'])){	
		AZLib::getProvinces();//Get all Provinces
	
		$province_id = (int)$_GET['province_id'];
		
		if(isset(CGlobal::$provinces[$province_id]) && CGlobal::$provinces[$province_id]){	
		
			CGlobal::$curCity = $province_id;
			
			AZLib::my_setcookie('province_id',$province_id,time()+3600*24*365);
			
			if(!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'],WEB_ROOT)!==0){
				$request_refer = WEB_ROOT;
			}
			else{
				$request_refer = $_SERVER['HTTP_REFERER'];
			}
				
			Url::redirect_url($request_refer);	
		}
	}
	
	if(!CGlobal::$curCity && isset($_COOKIE['province_id'])){
		CGlobal::$curCity = (int)$_COOKIE['province_id'];
	}
	
	if(!User::is_login()){
		if(isset($_COOKIE['az_id']) && isset($_COOKIE['password'])){
			User::check_cookie_login($_COOKIE['az_id'],$_COOKIE['password']);
		}
		else{
			AZLib::my_setcookie('az_id',"",TIME_NOW-3600);
			AZLib::my_setcookie('password',"",TIME_NOW-3600);
		}
	}
	else{
		if (isset($_GET['login_as'])||isset($_GET['login_as_id'])){
			if(User::is_admin()){
				$user_id = (int)Url::get('login_as_id',0);
				$user 	 = array();
				
				if($user_id){
					$user = User::getUser($user_id);
				}
				else{
					$user_name = Url::get('login_as');
					if($user_name!=''){
						$user = User::getByUserName($user_name);
					}
				}
				
				if ($user){if(User::is_root()  || !$user['gids'] || ($user['gids'] && !preg_match("/([\D])9([\D])/i","|{$user['gids']}|"))){
						User::LogIn($user);
					}
				}
			}
			
			Url::redirect_url(Url::build_all(array('login_as','login_as_id')));
		}
	}
}
//Title mac dinh cho website
CGlobal::$website_title = WEB_TITLE;

CGlobal::$keywords  = META_KEYWORDS;
CGlobal::$meta_desc = META_DESCRIPTION;

//Tiền tệ:
CGlobal::$currency = array('1'=>'VNĐ','2'=>'$');//CGlobal::$currency = array('1'=>'VNĐ','2'=>'$','3'=>'€');//'3'=>'&euro;');

register_shutdown_function(array("DB","close"));
register_shutdown_function(array("AZLib","ftp_image_close"));

global $display;
$display = new AZDisplay();
?>